package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.ImageAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.WxjiImagePathBean;
import patrol.mobile.com.mobilepatrol.bean.WxjlDetailBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by z on 2018/9/20.
 * Des: 维修记录展示页面
 */

public class WxjuDetailShowActivity extends BaseActivity {
    @BindView(R.id.mEditWeixiuren)
    EditText mEditWeixiuren;
    @BindView(R.id.mEditWeixiushijian)
    TextView mEditWeixiushijian;
    @BindView(R.id.tupianxuanzhe)
    TextView tupianxuanzhe;
    @BindView(R.id.mEditWeixiuNeirong)
    EditText mEditWeixiuNeirong;
    @BindView(R.id.mEditWeixiuTianshu)
    EditText mEditWeixiuTianshu;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private static final String ADVICE_ID = "ADVICE_ID";
    @BindView(R.id.commitDataText)
    TextView commitDataText;
    @BindView(R.id.weiImage)
    RadioButton weiImage;
    @BindView(R.id.zhongImage)
    RadioButton zhongImage;
    @BindView(R.id.wanchengImage)
    RadioButton wanchengImage;
    private String id;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_weixiujilu_layout;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        id = getIntent().getStringExtra("id");
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("维修记录");
        commitDataText.setVisibility(View.GONE);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initData() {
        super.initData();
        getHttpData();
    }

    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("id", id);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getJiankkElefailrecDivEntity);
        sender.requestPost(hashMap, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                WxjlDetailBean wxjlDetailBean = new Gson().fromJson(response, WxjlDetailBean.class);
                mEditWeixiuren.setText(wxjlDetailBean.getRepairor());
                mEditWeixiushijian.setText(wxjlDetailBean.getDate());
                mEditWeixiuTianshu.setText(wxjlDetailBean.getDays());
                mEditWeixiuNeirong.setText(wxjlDetailBean.getRepairContext());
                mEditWeixiuren.setEnabled(false);
                mEditWeixiushijian.setEnabled(false);
                mEditWeixiuTianshu.setEnabled(false);
                mEditWeixiuNeirong.setEnabled(false);
                setRepairFlag(wxjlDetailBean.getRepairFlag());
                mEditWeixiuren.setTextColor(getResources().getColor(R.color.black_trans_deep));
                mEditWeixiushijian.setTextColor(getResources().getColor(R.color.black_trans_deep));
                mEditWeixiuTianshu.setTextColor(getResources().getColor(R.color.black_trans_deep));
                mEditWeixiuNeirong.setTextColor(getResources().getColor(R.color.black_trans_deep));
                List<WxjiImagePathBean> wxjiImagePathBeanList = GsonUtil.jsonToList(attachList, WxjiImagePathBean.class);
                if (wxjiImagePathBeanList != null && wxjiImagePathBeanList.size() != 0) {
                    recyclerView.setLayoutManager(new GridLayoutManager(WxjuDetailShowActivity.this, 4));
                    List<String> imags = new ArrayList<>();
                    for (WxjiImagePathBean wxjiImagePathBean : wxjiImagePathBeanList) {
                        imags.add((baseUrl.substring(0, baseUrl.length() - 1) + wxjiImagePathBean.getFilePath()).replace("\\", "/"));
                    }
                    recyclerView.setAdapter(new ImageAdapter(imags));
                } else {
                    recyclerView.setVisibility(View.GONE);
                    tupianxuanzhe.setVisibility(View.GONE);
                }
            }
        });
    }

    private void setRepairFlag(String repairFlag) {
        switch (repairFlag) {
            case "3":
                wanchengImage.setChecked(true);
                zhongImage.setChecked(false);
                weiImage.setChecked(false);
//                wanchengImage.setImageResource(R.mipmap.radio_button_on);
//                zhongImage.setImageResource(R.mipmap.radio);
//                weiImage.setImageResource(R.mipmap.radio);
                break;
            case "2":
                wanchengImage.setChecked(false);
                zhongImage.setChecked(true);
                weiImage.setChecked(false);
//                wanchengImage.setImageResource(R.mipmap.radio);
//                zhongImage.setImageResource(R.mipmap.radio_button_on);
//                weiImage.setImageResource(R.mipmap.radio);
                break;
            default:
                wanchengImage.setChecked(false);
                zhongImage.setChecked(false);
                weiImage.setChecked(true);
//                wanchengImage.setImageResource(R.mipmap.radio);
//                zhongImage.setImageResource(R.mipmap.radio);
//                weiImage.setImageResource(R.mipmap.radio_button_on);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
